<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TMDT Project Admin</title>
<link rel="stylesheet" href="/admin/resources/css/reset.css"
	type="text/css" media="screen" />
<link rel="stylesheet" href="/admin/resources/css/style.css"
	type="text/css" media="screen" />
<link rel="stylesheet" href="/admin/resources/css/invalid.css"
	type="text/css" media="screen" />
<script type="text/javascript"
	src="/admin/resources/scripts/jquery-1.3.2.min.js"></script>
<script type="text/javascript"
	src="/scripts/jquery-1.8.3.min.js"></script>	
<script type="text/javascript"
	src="/admin/resources/scripts/simpla.jquery.configuration.js"></script>
<script type="text/javascript" src="/admin/resources/scripts/facebox.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery.wysiwyg.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery.datePicker.js"></script>
<script type="text/javascript"
	src="/admin/resources/scripts/jquery.date.js"></script>
	
</head>
<body>
<div id="body-wrapper"><!-- Wrapper for the radial gradient background -->

<div id="sidebar">
<div id="sidebar-wrapper"><!-- Sidebar with logo and menu -->

<h1 id="sidebar-title"><a href="#">TMDT Project Admin</a></h1>

<!--  Logo (221px wide) --> 
	<a href="#"><img id="logo"
	src="/admin/resources/images/logo.png" alt="Simpla Admin logo" /></a>

			
			<?php include_once 'all_auctions_main_nav.php'?>
			

			
		</div>
</div>
<!-- End #sidebar -->
<div id="clear"></div>
<div id="main-content"><!-- Main Content Section with everything -->
	<?php include_once '../shortcut_buttons.php' ?>
		<?php 
		include_once '../config.php' ?>
<noscript>
<!-- Show a notification if the user has disabled javascript -->
<div class="notification error png_bg">
<div>Javascript is disabled or is not supported by your browser. Please
<a href="http://browsehappy.com/" title="Upgrade to a better browser">upgrade</a>
your browser or <a
	href="http://www.google.com/support/bin/answer.py?answer=23852"
	title="Enable Javascript in your browser">enable</a> Javascript to
navigate the interface properly.</div>
</div>
</noscript>

<?php
		include_once '../config.php';
		$auctionID="";
		$nameErr = $productIDErr = $quantityErr  = $end_timeErr= $start_timeErr= "";
		$auction_name = $product_id = $quantity = $start_time =$end_time =$price=$price_per_bid= "";
		$message ="";
		$err= false;
	 
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		$auctionID= $_POST['id'];
	    if (empty($_POST["auction_name"])) {
	        $nameErr = "Auction name is required.";
			$err=true;

	       
	    }
	    else {
	        $auction_name = trim($_POST["auction_name"]);
	    }
	 
	    if (empty($_POST["product_id"])) {
	        $productIDErr = "Product ID is required.";
	        $err =true;

	    }
	    else {
	        $product_id = trim($_POST["product_id"]);
	    }
	 
	    if (empty($_POST["quantity"]))  {
	        $quantityErr = "Quantity is required.";
	        $err =true;

	    }
	    else {
	        $quantity = trim($_POST["quantity"]);
	    }
	 
	    if (empty($_POST["start_time"]) ) {
	        $start_timeErr = "Start time is required.";
	        $err= true;
	    }
	    else {
	        $start_time = trim($_POST["start_time"]);
	    }
	    
		if (empty($_POST["end_time"]) ) {
	        $end_timeErr = "End time is required.";
	        $err= true;

	    }
	    else {
	        $end_time = trim($_POST["end_time"]);
	    }	 
	    
	    if (empty($_POST["price"]) || trim(strtoupper($_POST["price"])== "DEFAULT")) {
	        $price = 0;
	    }
	    else {
	        $price = trim($_POST["price"]);
	    }	
	     
		if (empty($_POST["price_per_bid"]) || trim(strtoupper($_POST["price_per_bid"])== "DEFAULT")) {
	        $price_per_bid = 1000;
	    }
	    else {
	        $price_per_bid = trim($_POST["price_per_bid"]);
	    }
				
		//////////////////////////////////////////////////
		//Insert Auction into Database
		//////////////////////////////////////////////////
		if ($err == true) {
		?>
				
				<div class="notification error png_bg">
					<a href="#" class="close"><img src="/admin/resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close"/></a>
					<div>
						Validate Error: <?php echo "$nameErr  $productIDErr  $quantityErr $start_timeErr  $end_timeErr"?>	
					</div>
				</div>	

				<?php 			
		}
		else{
			
			$connection = mysql_connect ( $host, $username, $password ) or die ( "Could not connect to MySql" . mysql_error () );
			
			mysql_select_db ( $db, $connection ) or die ( "Could not connect to MySql" . mysql_error () );
	
				$statement = "UPDATE Auction SET  name='$auction_name',productID = '$product_id',quantity = $quantity,end_time = STR_TO_DATE('$end_time','%d-%m-%Y %H:%i:%s'),start_time = STR_TO_DATE('$start_time','%d-%m-%Y %H:%i:%s'),auction_price = $price,price_per_bid = $price_per_bid where auctionID = $auctionID" ;
			//die($statement);
			$result= mysql_query($statement);
			
			if (! $result) {
				$message = "MySQL: Error " . mysql_errno () . " : " . mysql_error ();
		?>
				
				<div class="notification error png_bg">
					<a href="#" class="close"><img src="/admin/resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close"/></a>
					<div>
						<?php echo $statement."  ".$message; ?>	
					</div>
				</div>	

				<?php 
			}
			else {
				$message = "Updatet Aution Successful";
				?>
			<div class="notification success png_bg">
				<a href="#" class="close"><img src="/admin/resources/images/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a>
				<div>
					<?php echo $message; ?>	
				</div>
			</div>

				<?php 
			}
			mysql_close($connection);
		}
	}
	else if ($_SERVER["REQUEST_METHOD"] == "GET" && isset($_GET['id']) && !empty($_GET['id'])){
			$auctionID = $_GET['id'];
		?><!-- 
		 -->
	<div class="content-box-content">

<div class="tab-content current" id="update_auction_tab">

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
	method="post">

<fieldset><!-- Set class to "column-left" or "column-right" on fieldsets to divide the form into columns -->
<?php 
			$connection = mysql_connect ( $host, $username, $password ) or die ( "Could not connect to MySql" . mysql_error () );
			mysql_select_db ( $db, $connection ) or die ( "Could not connect to MySql" . mysql_error () );
			$result = @mysql_query ( "select * from auction where auctionID=$auctionID", $connection );
			$row = @mysql_fetch_assoc ( $result ) ;
				$auction_name = $row['name'];
				$product_id = $row['productID'];
				$quantity = $row['quantity'];
				$start_time = $row['start_time'];
				$end_time = $row['end_time'];
				$price = $row['auction_price'];
				$price_per_bid = $row['price_per_bid'];
			
?>
<p><label>Auction Name(*)</label> <input
	class="text-input medium-input datepicker" type="text"
	id="auction_name" name="auction_name" value= "<?php echo htmlspecialchars($auction_name);?>"/> 
	<span id="auction_name_message" class = "validate_err" ><?php echo $nameErr?></span> <!-- Classes for input-notification: success, error, information, attention -->
<br />
<small>Name for this auction</small></p>
<p>
	<label>Product(*)</label>              
	<select class="small-input" name="product_id" id="product_id">

	<?php 
	
		$result = mysql_query("select name,productID from Product where productID = '$product_id'",$connection);
		$selected = mysql_fetch_array($result,MYSQL_ASSOC)
	?>
		<option value=<?php echo htmlspecialchars("'".$selected["productID"]."'")?>><?php echo $selected["name"]?></option>
	

  	
		<?php 
			$result1 = mysql_query("select name,productID from Product where productID != '$product_id'",$connection);
			
			while ($row = mysql_fetch_array($result1,MYSQL_ASSOC)){
				
		?>
		<option value=<?php echo htmlspecialchars("'".$row["productID"]."'")?>><?php echo $row["name"]?></option>
		<?php 
			}
			mysql_close($connection);
		?>
	</select> 
	<span id="product_id_message" class = "validate_err"><?php echo $productIDErr?></span>
</p>


<p><label>Quantity(*)</label> 
<input class="text-input small-input"
	type="text" id="quantity" name="quantity" value = "<?php echo htmlspecialchars($quantity);?>"/> 
	<span id="quantity_message" class = "validate_err"><?php echo $quantityErr?></span> <!-- Classes for input-notification: success, error, information, attention -->
<br />
<small>Number of products are avaiable for this auction</small></p>
<p><label>Start Time(*)</label> <input class="text-input small-input"
	type="text" id="start_time" name="start_time" 
	
	value= "<?php
	$date = date_create($start_time);
	 echo htmlspecialchars(date_format($date,'d-m-Y H:i:s'));
	 
	 ?>"/>
	 <span id="start_time_message"><?php $start_timeErr ?></span> <br />
<small>Format: DD-MM-YYYY HH:MM:SS.</small></p>


<p><label>End Time(*)</label> <input class="text-input small-input"
	type="text" id="end_time" name="end_time" 
	value= "<?php 
	$date = date_create($end_time);
	 echo htmlspecialchars(date_format($date,'d-m-Y H:i:s'));
	?>"/> 
	<span id="end_time_message" class = "validate_err"><?php echo $end_timeErr?></span> <br />
<small>The auction will be closed at this time</small> <br />
<small>Format: DD-MM-YYYY HH:MM:SS.</small></p>

<p><label>Price</label> <input class="text-input small-input"
	type="text" id="price" name="price" value= "<?php echo htmlspecialchars($price);?>"/>
	 <span id="upset_price_message"></span> <!-- Classes for input-notification: success, error, information, attention -->
<br />
<small>Price for this auction. Blank: 0</small></p>

<p><label>Price per Bid</label> <input class="text-input small-input"
	type="text" id="price_per_bid" name="price_per_bid" value= "<?php echo htmlspecialchars($price_per_bid);?>"/> 
	<span id="price_per_bid_message"></span> <!-- Classes for input-notification: success, error, information, attention -->
<br />
<small>This value will be add to the auction price after one bid from
user. Blank: 1000</small></p>

<input name='id' value = '<?php echo htmlspecialchars($auctionID);?>'/>
<p><input class="button" type="submit" value="Update" /> <a
	href="/admin/pages/allauctions.php"><input class="button" value="Cancel" href="/admin/pages/allauctions.php"/> </a></p>

</fieldset>

<div class="clear"></div>
<!-- End .clear -->

</form>

</div>
<!-- End #tab2 --></div>
<!-- End .content-box-content --></div>
<!-- End .content-box -->


<div class="clear"></div>
		<?php }
		else{
			header("Location: /admin/allauctions.php");
		};
		
	?>